package com.thundersoft.musicbox;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;

/**
 * 备注:
 *
 * @author hmb
 * @version 1.0
 * @since 2015/11/19 12:11
 */
public class PlayingFragment extends Fragment implements View.OnClickListener {
    private ProgressBar progressBar = null;
    private ImageButton play;
    private ImageButton pause;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_playing, container, false);

        initView(rootView);
        return rootView;
    }

    private void initView(View view) {
        progressBar = (ProgressBar) view.findViewById(R.id.progressBar);

        ImageButton backward = (ImageButton) view.findViewById(R.id.btn_backward);
        backward.setOnClickListener(this);

        ImageButton forward = (ImageButton) view.findViewById(R.id.btn_forward);
        forward.setOnClickListener(this);

        ImageButton last = (ImageButton) view.findViewById(R.id.btn_last);
        last.setOnClickListener(this);

        ImageButton next = (ImageButton) view.findViewById(R.id.btn_next);
        next.setOnClickListener(this);

        play = (ImageButton) view.findViewById(R.id.btn_play);
        play.setOnClickListener(this);

        pause = (ImageButton) view.findViewById(R.id.btn_pause);
        pause.setOnClickListener(this);

        MusicManager manager = MusicApplication.getMusicManager();
        if (manager != null && manager.isPlaying()) {
            setPlayState(true);
        }

    }


    @Override
    public void onClick(View v) {
        MusicManager manager = MusicApplication.getMusicManager();
        switch (v.getId()) {
            case R.id.btn_backward:
                showToast("后退");
                break;
            case R.id.btn_forward:
                showToast("前进");
                break;
            case R.id.btn_last:
                showToast("上一首");
                manager.last();
                setPlayState(true);
                break;
            case R.id.btn_next:
                showToast("下一首");
                manager.next();
                setPlayState(true);
                break;
            case R.id.btn_play:
                showToast("播放");
                manager.play();
                setPlayState(true);
                break;
            case R.id.btn_pause:
                showToast("暂停");
                manager.pause();
                setPlayState(false);
                break;
            default:
                break;

        }
    }

    private void setPlayState(boolean isPlaying) {
        if (isPlaying) {
            play.setVisibility(View.GONE);
            pause.setVisibility(View.VISIBLE);
        } else {
            play.setVisibility(View.VISIBLE);
            pause.setVisibility(View.GONE);
        }
    }

    private void showToast(String s) {
        Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
    }


}
